﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 通讯录
{
    static class Program
    {
        public static JObject result = new JObject();

        public static object LBPointdelay { get; private set; }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            ReadData();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Helloworld1());
        }

        public struct DataInfo
        {
            public long id;
            public string name;
            public double phoneNumber;
        }

        public static List<DataInfo> userLs = new List<DataInfo>();
        public static List<DataInfo> serachUserLs = new List<DataInfo>();

        static void ReadData()
        {
            if (!File.Exists("config.json"))
            {
                File.WriteAllText("config.json", "");
            }
            string json = File.ReadAllText("config.json");
            if (!string.IsNullOrEmpty(json))
            {
                userLs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DataInfo>>(json);
            }
            if (userLs == null) userLs = new List<DataInfo>();
            serachUserLs = userLs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="state">0:add, 1:removed, 2:modify</param>
        public static void WriteData(DataInfo data, byte state = 0)
        {
            try
            {
                if (!File.Exists("config.json"))
                {
                    File.WriteAllText("config.json", "");
                }  
                switch (state)
                {
                    case 0:
                        if (data.id <= 0)
                        {
                            data.id = DateTime.Now.Ticks;
                        }
                        userLs.Add(data);
                        break;
                    case 1:
                        for (int i = 0; i < userLs.Count; i++)
                        {
                            if (userLs[i].id == data.id)
                            {
                                userLs.RemoveAt(i);
                                break;
                            }
                        }
                        break;
                    case 2:
                        for (int i = 0; i < userLs.Count; i++)
                        {
                            if (userLs[i].id == data.id)
                            {
                                userLs[i] = data;
                                break;
                            }
                        }
                        break;
                }

                string output = Newtonsoft.Json.JsonConvert.SerializeObject(userLs, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText("config.json", output);
            }
            catch
            {
                MessageBox.Show("数据异常！");
            }
        }
    }
}
